package com.bianmaba.utils;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * 输入输出流工具类
 *
 * @author cWX183898
 */
public final class StreamUtils {
    public static final Logger LOG = LoggerFactory.getLogger(StreamUtils.class);

    private StreamUtils() {
    }

    /**
     * 关闭不为空的CloseableHttpResponse
     *
     * @param response CloseableHttpResponse
     */
    public static void close(CloseableHttpResponse response) {
        try {
            if (response != null) {
                response.close();
            }
        } catch (IOException e) {
            LOG.error("关闭HttpResponse出现异常.", e);
        }
    }

    /**
     * 关闭不为空的CloseableHttpClient,
     *
     * @param client CloseableHttpClient
     */
    public static void close(CloseableHttpClient client) {
        try {
            if (client != null) {
                client.close();
            }
        } catch (IOException e) {
            LOG.error("关闭HttpClient客户端出现异常.", e);
        }
    }

    /**
     * 关闭不为空的BufferedOutputStream,
     *
     * @param out BufferedOutputStream
     */
    public static void close(BufferedOutputStream out) {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            LOG.error("关闭带缓存的输出流出现异常.", e);
        }
    }

    /**
     * 关闭不为空的OutputStreamWriter,
     *
     * @param out OutputStreamWriter
     */
    public static void close(OutputStreamWriter out) {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            LOG.error("关闭输出流写入器出现异常.", e);
        }
    }

    /**
     * 关闭不为空的OutputStream,
     *
     * @param out OutputStream
     */

    public static void close(OutputStream out) {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            LOG.error("关闭输出流出现异常.", e);
        }
    }

    /**
     * 关闭不为空的Writer,
     *
     * @param writer Writer
     */
    public static void close(Writer writer) {
        try {
            if (writer != null) {
                writer.close();
            }
        } catch (IOException e) {
            LOG.error("关闭写入器出现异常.", e);
        }
    }

    /**
     * 关闭不为空的BufferedInputStream,
     *
     * @param val BufferedInputStream
     */
    public static void close(BufferedInputStream val) {
        try {
            if (val != null) {
                val.close();
            }
        } catch (IOException e) {
            LOG.error("关闭带缓存的输入流出现异常.", e);
        }
    }

    /**
     * 关闭不为空的InputStreamReader,
     *
     * @param val InputStreamReader
     */
    public static void close(InputStreamReader val) {
        try {
            if (val != null) {
                val.close();
            }
        } catch (IOException e) {
            LOG.error("关闭输出流读取器出现异常.", e);
        }
    }

    /**
     * 关闭不为空的ByteArrayOutputStream,
     *
     * @param val ByteArrayOutputStream
     */
    public static void close(ByteArrayOutputStream val) {
        try {
            if (val != null) {
                val.close();
            }
        } catch (IOException e) {
            LOG.error("关闭字节数组输出流出现异常.", e);
        }
    }

    /**
     * 关闭不为空的ByteArrayInputStream,
     *
     * @param val ByteArrayInputStream
     */
    public static void close(ByteArrayInputStream val) {
        try {
            if (val != null) {
                val.close();
            }
        } catch (IOException e) {
            LOG.error("关闭字节数组输入流出现异常.", e);
        }
    }

    /**
     * 关闭不为空的InputStream,
     *
     * @param val InputStream
     */
    public static void close(InputStream val) {
        try {
            if (val != null) {
                val.close();
            }
        } catch (IOException e) {
            LOG.error("关闭输出流出现异常.", e);
        }
    }

    /**
     * 关闭不为空的Reader,
     *
     * @param reader Reader
     */
    public static void close(Reader reader) {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            LOG.error("关闭写入器出现异常.", e);
        }
    }

}
